



var app = getApp();
const quote = app.globalData.quote;
const api = quote.api;
const tool = quote.tool;
const innerAudioContext = app.globalData.innerAudioContext; //引入音频播放器
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // animation:null,//引导页动画
        isShowGuidance:true,//是否显示引导页
        taskStatus:app.globalData.taskStatus,//任务状态
        imgUrl:'',
        // imgUrl:app.globalData.imgUrl,
        questionArr: [], //打乱的单词数组
        answerStr: '', //正确答案的字符串
        userAnswer: [], //用户选择单词后的数组
        isBrang: app.globalData.isBrang,
        isShowTip: false, //是否通关完毕
        isTrue: false, //是否排序正确
        taskAllData: null, //请求的全部问题数据
        taskDataIndex: 0, //正在通关的题数
        isShowEndTip: false, //任务结束弹窗
        isClickContinue: false,//防止连续点击两次
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        // that.endTip = this.selectComponent("#endTip");
        console.log(app.globalData.taskStatus)
        that.setData({
            taskStatus: app.globalData.taskStatus,//任务状态
        })
        that.getTaskOneData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        var that = this

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that=this
        // setTimeout(() => {
        //     that.setData({
        //         isShowGuidance: false,
        //     })     
        // }, 2000)
        console.log(that.data.isBrang)

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            path: 'pages/indexPackage/index/index',
            success: function (res) {
                console.log('转发成功', res);
            },
            fail: function (err) {
                console.log('转发失败', err)
            }
        }
    },
    beginTask:function(){
        var that=this
        that.setData({
            isShowGuidance: false,
        })
    },
    //获取任务一的数据
    getTaskOneData: function() {
        var dataDic = {
            ModuleId:app.globalData.moduleId,
            TaskMark:1
        };
        var that = this
        api.methodBasicEnglish_GetTaskData({
            dataDic: dataDic,
            success: function(res) {
                console.log(res)
                var data = res.data.data.TaskList
                data.forEach(item => {
                    item.ImgUrl = app.globalData.imgUrl + item.ImgUrl
                })
                that.setData({
                    questionArr: data[that.data.taskDataIndex].EnglishSentenceList,
                    answerStr: data[that.data.taskDataIndex].EnglishSentence,
                    taskAllData: data,
                    imgUrl: data[that.data.taskDataIndex].ImgUrl
                })
            },
            fail:function(err){
                console.log('数据请求失败',err)
            }
        })
    },
    //结束任务
    endTask: function() {
        var that = this
        that.setData({
            isShowEndTip: true
        })
    },
    //用户点击取消
    cancal: function() {
        var self = this
        self.setData({
            isShowEndTip: false
        })
        // self.endTip.hidePopup();   
    },
    //用户点击确定
    confirm: function() {
        var that = this
        tool.statistics(app.globalData.openId, app.globalData.moduleId,3,1,1)
        wx.redirectTo({
            url: '../../../../pages/indexPackage/courseList/courseList',
        })
    },
    //点击下方打乱的单词数组
    clickQ: function(e) {
        var that = this
        if (that.data.isClickContinue) {
            console.log('防止连续点击')
            return
        }
        console.log(e)
        var arr = that.data.userAnswer
        var idex = e.currentTarget.dataset.num
        var questionArr = that.data.questionArr
        //用字符串代替原有的数组 尽量保持位置不变
        questionArr.splice(idex, 1, '')
        that.setData({
            questionArr: questionArr,
            isClickContinue:true
        })
        var text = e.currentTarget.dataset.text
        var json = {
            text: text,
            id: idex //点击还原原来位置的标识
        }
        //将点击的单词保存到一个数组 
        arr.push(json)
        that.setData({
            userAnswer: arr
        })
        console.log(arr, that.data.userAnswer)
        setTimeout(() => {
            var cArr = that.data.questionArr.join('')
            console.log('拼接的字符串', cArr)
            
            if (cArr == "") {
                console.log('点完全部，判断答案', that.data.questionArr, that.data.userAnswer)
                var d = []
                //拼接用户选择的数组为字符串
                for (var j = 0; j < that.data.userAnswer.length; j++) {
                    d.push(that.data.userAnswer[j].text)
                }
                var c = d.join(' ')
                console.log(c)
                that.setData({
                    isClickContinue: false
                })
                if (c == that.data.answerStr) {
                    wx.showToast({
                        title: '答对了',
                        icon: 'success',
                        duration:1000,
                        mask: true
                    })
                    innerAudioContext.src = '/src/record/true1.mp3'
                    innerAudioContext.play()
                    console.log('答案是对的')
                    if (that.data.taskDataIndex < that.data.taskAllData.length - 1) {
                        //判断是否答完题目
                        that.setData({
                            isTrue: true,
                            isSelectedAll: true,
                            taskDataIndex: that.data.taskDataIndex + 1,
                        })
                        setTimeout(() => {
                            that.setData({
                                isSelectedAll: false,
                                questionArr: that.data.taskAllData[that.data.taskDataIndex].EnglishSentenceList,
                                answerStr: that.data.taskAllData[that.data.taskDataIndex].EnglishSentence,
                                // imgUrl: app.globalData.imgUrl + that.data.taskAllData[that.data.taskDataIndex].ImgUrl,
                                imgUrl:that.data.taskAllData[that.data.taskDataIndex].ImgUrl,
                                userAnswer: []
                            })
                        }, 1200)
                    } else {
                        setTimeout(() => {
                            that.setData({
                                isShowTip: true
                            })
                        }, 1200)
                    }

                } else {
                    wx.showToast({
                        title: '答错了',
                        image: '/src/images/popup_close.png',
                        duration: 1000,
                        mask: true
                    })
                    console.log('答案是错的')
                    innerAudioContext.src = '/src/record/false.mp3'
                    innerAudioContext.play()
                    that.setData({
                        isTrue: false,
                        isSelectedAll: true
                    })
                    setTimeout(() => {
                        that.setData({
                            questionArr: that.data.taskAllData[that.data.taskDataIndex].EnglishSentenceList,
                            userAnswer: [],
                            isSelectedAll: false
                        })
                    }, 1200)
                }
            }else{
                console.log('还没点完')
                that.setData({
                    isClickContinue: false
                })
            }
        }, 200)



    },
    //点击选择好的单词
    clickA: function(e) {
        var that = this
        console.log(e)
        //原来数组的下标
        var num = e.currentTarget.dataset.num
        var index = e.currentTarget.dataset.index
        var text = e.currentTarget.dataset.text
        var questionArr = that.data.questionArr
        questionArr.splice(num, 1, text)
        that.setData({
            questionArr: questionArr
        })
        var userAnswer = that.data.userAnswer
        userAnswer.splice(index, 1)
        console.log(userAnswer)
        that.setData({
            userAnswer: userAnswer
        })
    },
    //通关
    nextOne: function() {
        var that=this
        console.log('判断跳转')
        console.log(that.data.taskStatus)
        if (that.data.taskStatus.TaskTwo == true) {
            wx.navigateTo({
                url: '/pages/taskPackage/pages/taskTwo/taskTwo',
            })
            return
        } else if (that.data.taskStatus.TaskThree == true) {
            wx.navigateTo({
                url: '/pages/taskPackage/pages/taskThree/taskThree',
            })
            return
        } else if (that.data.taskStatus.TaskFour == true) {
            wx.navigateTo({
                url: '/pages/taskPackage/pages/taskFour/taskFour',
            })
            return
        }else{
          
        }
    }
    
})